window.onload=function () {
	show('mid-form');
	getusername();
	getuser();
	getmycar();
}
function show(argument) {
	$('#mid-form').css('display', 'none');
	$('#mid-table').css('display', 'none');
	$('#mid-password').css('display', 'none');
	$('#mid-emailandphone').css('display','none');
	$('#'+argument+'').css('display','block');
}
function getuser() {
	$.ajax({
		url: 'http://172.18.21.150/ToGo/getUser',
		type: 'post',
		dataType: 'json',
		data: {username: $.cookie('username')},
		success:function (result) {
			if(result.status=='200')
			{
				setValue(result.data);
			}
		}
	});
	
}
function setValue(obj){    
    // 开始遍历     
    for(var p in obj){        
       // 方法    
       console.log(obj);  
       if(typeof(obj[p])=="function"){        
           // obj[p]();       
        }else{      
            $("#"+p).html(obj[p]);  
            // p 为属性名称，obj[p]为对应属性的值       
                
        }        
    }        
}

function revisepassword(){
	// alert( $.cookie('username')+$('#oldpassword').val()+$('#newpassword').val());
	$.ajax({
		url: 'http://172.18.21.150/ToGo/changePassword',
		type: 'post',
		dataType: 'json',
		data: {username: $.cookie('username'),oldPassword:$('#oldpassword').val(),newPassword:$('#newpassword').val(),},
		success:function (result) {
			if(result.status=='200'){
				alert("修改成功！");
			}
		},
		error:function () {
			alert("修改密码错误！");
		}
	});
}
function revisemail(){
	$.ajax({
		url: 'http://172.18.21.150/ToGo/changeEmail',
		type: 'post',
		dataType: 'json',
		data: {user_id: $.cookie('userid'),email:$('#newEmail').val()},
		success:function (result) {
			if(result.status=='200'){
				alert("修改成功！");
			}
		},
		error:function () {
			alert("修改邮箱错误！");
		}
	});
}
function revisephone(){
	// alert($.cookie('userid')+$('#newPhoneNumber').val());
	$.ajax({
		url: 'http://172.18.21.150/ToGo/changePhoneNumber',
		type: 'post',
		dataType: 'json',
		data: {user_id: $.cookie('userid'),phoneNumber:$('#newPhoneNumber').val()},
		success:function (result) {
			if(result.status=='200'){
				alert("修改成功！");
			}
		},
		error:function () {
			alert("修改密码错误！");
		}
	});
}


function changetohavesell(car_id) {
  $.ajax({
    url: 'http://172.18.21.150/ToGo/changeCarToSold',
    type: 'post',
    dataType: 'json',
    data:{car_id:car_id},
    success:function (result) {
      if (result.status=='200') {
        window.location.reload(true);
      }
    },
    error:function(){
      alert('error');
    }
  });
}

function getmycar() {
	 $.ajax({
    url: 'http://172.18.21.150/ToGo/getCarByUser',
    type: 'post',
    dataType: 'json',
    data:{user_id:$.cookie('userid'),pageNum:1,pageSize:10},
    success:function (result) {
      if (result.status=='200') {
				
        var data=result.data;
        for (var i = 0; i < data.length; i++) {
        	var status;
          	 if(data[i].status=='0'){
           		status='待审核';
         	  }else if(data[i].status=='1'){
           		status='在售';
         	  }else{
         	  	status='已售';
         	  }
         	  $('#mid-table').append('<tr><td class="mid-table-brand">'+data[i].brand+'</td><td class="mid-table-model">'+data[i].model+'</td><td class="mid-table-mileage">'+data[i].mileage+'</td><td class="mid-table-price">'+data[i].price+'</td><td class="mid-table-photo"><img src="http://172.18.21.150/'+data[i].photo+'" style="width:100px;height:100px;"></td><td class ="mid-table-status">'+status+'</td><td class ="mid-table-modify"><input type="button" value="已售" class="changesellstatus" onclick="changetohavesell('+data[i].car_id+')"></td></tr>')
        }

      }
    },
    error:function(){
      alert('error');
    }
  });
}
